<template>
  <el-dialog
    v-el-drag-dialog
    title="订单详情"
    :visible.sync="visible"
    width="450px"
    @closed="reset"
  >
    <el-form ref="dataForm" class="form" :model="temp" label-position="right" label-width="80px">
      <el-form-item label="订单号" prop="orderCode">
        <el-input v-model="billInfo.orderCode" disabled />
      </el-form-item>
      <el-form-item label="交易金额" prop="taccReceivable">
        <el-input v-model="billInfo.taccReceivable" disabled />
      </el-form-item>
      <el-form-item label="当前机构" prop="orgName">
        <el-input v-model="temp.orgName" disabled />
      </el-form-item>
      <el-form-item label="签收机构" prop="orgName">
        <el-input v-model="billInfo.orgName" disabled />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="success">
        确认转入
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { submitCashTransaction, queryBillInfoData } from '@/api'

  export default {
    name: 'detail',
    data() {
      return {
        visible: false,
        temp: {
          billCode: '',
          cashFee: '',
          orgName: '',
          addScores: ''
        },
        billInfo: {
          orderCode: '',
          taccReceivable: '',
          orgName: ''
        }
      }
    },
    methods: {
      async open(temp) {
        await this.getBillInfo(temp.billCode)
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.temp = Object.assign(this.temp, temp)
          }
        })
      },
      async getBillInfo(billCode) {
        const res = await queryBillInfoData({
          billCode
        })
        this.billInfo = res.data
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          const params = {}
          params.recordId = this.temp.recordId
          params.billCode = this.billInfo.billCode
          const orgId = this.billInfo.orgId
          if (!orgId) {
            this.$message.warning('转入机构为空，该订单可能没有所属机构')
            return
          }
          params.orgId = orgId
          params.orgName = this.billInfo.orgName
          params.orgSNNo = this.billInfo.orgSNNo
          params.billType = this.temp.billType
          this.submit(params)
        })
      },
      async submit(params) {
        this.$confirm(`确定转入吗？`, '提示', {
          type: 'warning'
        }).then(() => {
          const loading = this.$message({
            message: '正在执行操作',
            duration: 0,
            iconClass: 'el-icon-loading'
          })
          submitCashTransaction(params).then(() => {
            this.resolve()
            this.visible = false
            this.$notify({
              title: '提示',
              message: '转入成功',
              type: 'success',
              duration: 2000
            })
          }).finally(() => {
            loading.close()
          })
        })
      },
      reset() {
        this.temp = {
          billCode: '',
          cashFee: '',
          orgName: '',
          addScores: ''
        }
        this.billInfo = {
          orderCode: '',
          taccReceivable: '',
          orgName: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.info-title {
  display: flex;
  align-items: center;
  margin-top: 10px;
  margin-bottom: 20px;
  .title-icon {
    display: inline-block;
    width: 4px;
    height: 18px;
    margin-right: 6px;
    background: $themeColor;
  }
  span {
    font-weight: 700;
    color: $themeColor;
  }
}
</style>
